<?php

/**
 * @author Martin Pernica
 * @copyright 2009
 */

class Content extends System
{

    public function __construct()
    {
    }
    /*
    TODO : Redirect
    public function _redir($where){
    header('');

    }
    */
    public function getLinkOfArticle($id)
    {
        $query = parent::$db->query("SELECT id ,title FROM {prefix}articles WHERE id = '" .
            intval($id) . "'");
        $data = $query->fetch_array();
        $data = 'content/' . $data['id'] . '-' . self::parseUrl($data['title']);

        return $data;
    }

    public function getTitleOfArticle($id)
    {
        $query = parent::$db->query("SELECT title FROM {prefix}articles WHERE id = '" .
            intval($id) . "'");
        $data = $query->fetch_array();

        return $data['title'];
    }
    
    public function getCategoryName($id)
    {
        $query = parent::$db->query("SELECT name FROM {prefix}categories WHERE id = '".intval($id)."' LIMIT 1");
        $data = $query->fetch_object();
        
        return $data->name;
     }

    public static function parseTlp($component, $filename)
    {
        $user_template = APPROOT . DIR_SEP . 'templates' . DIR_SEP . $GLOBALS['setting']['template'] .
            DIR_SEP . 'components' . DIR_SEP . $component . DIR_SEP . $filename;
        $template = APPROOT . DIR_SEP . 'components' . DIR_SEP . $component . DIR_SEP .
            'templates' . DIR_SEP . $filename;

        ob_start();

        if (is_file($user_template)) {
            include $user_template;
        } else {
            include $template;
        }

        $buffer = ob_get_contents();
        ob_end_clean();
        return $buffer;
    }

    public static function paginator($table, $query, $p = 10, $str = 1)
    {
        $result = Registry::get('db')->query("SELECT COUNT(0) as COUNT_TABLE FROM {prefix}" .
            $table);
        $count = $result->fetch_array();
        $count = $count[0];

        $start = ($str - 1) * $p;

        $out = "";

        $out .= "<div class=\"paginator\"><a href=\"{url}index.php?q=" . $query . ($str > 1 ?
            $str - 1 : "1") . "\">&laquo;</a>&nbsp";

        $all = (($count % $p) > 0) ? (int)($count / $p) + 1 : $count / $p;

        for ($i = 1; $i <= $all; $i++) {
            if ($i != $str) {
                $out .= "<a href=\"{url}index.php?q=" . $query . $i . "\">" . $i . "</a>&nbsp;";
            } else {
                $out .= "<a href=\"{url}index.php?q=" . $i . "\"><strong class=\"paginator_active\">" . $i . "</strong></a>&nbsp;";
            }
        }

        $out .= "<a href=\"{url}index.php?q=" . $query . (($str < $all) ? $str + 1 : $all) . "\">&raquo;</a>" .
            "</div>";
            
        $return = array($out, $start);
        
        return $return;

    }

    public static function escapeTemplateChars($text)
    {
        $text = str_replace(array('{', '}'), array('&#123;', '&#125;'), $text);
        return $text;
    }

}

?>